<?php
class Product_Service_RmaProccess
{
	protected $_order = null;	 
	protected $_orderCode = null;
	protected $_plan = null;
	protected $_procCnt  = null;
	protected $_products = null;
	protected $_customerId = null;
	protected $_customerInfo = null;
	
	public function __construct($type=null,$value=null){			
		if(isset($value) && isset($type)){			
			$this->setCustomer($type,$value);
		}
	}
	
	
	/**
	 * 参数 $type,类型值为 orders_code 或 receiving_code
	 * 通过orders_code 或 receiving_code 查询客户信息
	 */
	private function setCustomer($type=null,$value=null){
		
		if(!isset($this->_customerId)){
			if(isset($type) && $type =='orders_code'){
				$Info = Order_Service_Order::getByOrdersCode($value);	//订单信息
			}elseif (isset($type) && $type =='receiving_code'){
				$Info = Product_Service_Receiving::getByReceivingCode($value);	//订单信息				
			}
			if (isset($Info['customer_id']) && $Info['customer_id'] !=''){
				$this->_customerId = $Info['customer_id'];
				$customerObj = new Customer_Service_Customer();	
				$customerInfo = $customerObj->getByCustomerId($this->_customerId);				
				$this->_customerInfo = $customerInfo;
			}
		}
	
	}
	
	public function getCustomerInfo(){
		return $this->_customerInfo;
	}
	
	/**
	 * 创建一条新的 rma 信息
	 * 参数 $productArr: 一维数组,索引为产品id，值为 数量
	 */
	public function create($productArr,$row){
		if($this->_customerInfo['return_package_proccess'] == 0){
			return array('ask'=>0,'receiving_code'=>'','msg'=>array('用户处理方式为销毁'));
		}
		if(!$row['ref_id']){
			return array('ask'=>0,'receiving_code'=>'','msg'=>array('Please fill the Reference No.'));
		}elseif (!$this->checkReferenceNo($row['ref_id'])){
			return array('ask'=>0,'receiving_code'=>'','msg'=>array('Reference No existed'));
		}
		$code = $this->createCode();	// 返回一个新的相应rma code 
		$code && $row['receiving_code'] = $code;
		$row['customer_id'] = $this->_customerId;
		$Obj = new Product_Model_Receiving();
		$Obj->getAdapter()->beginTransaction();
		
		$rowLog =array();
		$rowLog['receiving_code'] = $code;
		$rowLog['rl_type'] = 0;			//0:内容修改;1:状态修改
		$rowLog['user_id'] = 0;			//用户新创建时
		$rowLog['customer_id'] = $this->_customerId;
		$rowLog['rl_note'] = '';		//待确定
		$rowLog['rl_add_time'] = date('Y-m-d H:i:s');
		$rowLog['rl_ip'] = Common_Service_Common::getIP();
		try{
			$lastInsertId = $Obj->add($row);
			$this->createItem($productArr,$code);
			$this->createRmaLog($rowLog);			//创建日志
			$Obj->getAdapter()->commit();
		}catch (Exception $e) {
			echo $e->getTraceAsString();
			echo $e->getMessage();
			$Obj->getAdapter()->rollBack();
			return array('ask'=>0,'receiving_code'=>'','msg'=>array('创建失败'));
		}
		$arr =  array('ask'=>'Success','receiving_code'=>$code,'msg'=>array('创建成功'));
		return $arr;
	}
	
	public function createItem($productArr,$code){
		if(!$productArr || !$code){
			throw new Exception('productId or receiving_code error.');
		}
		$cdate = date('Y-m-d H:i:s');
		$itemIdArr = array();
		foreach ($productArr as $key =>$val ){
			$itemArr = array();
			$itemArr['receiving_code'] = $code;
			$itemArr['product_id'] = $key;
			$itemArr['order_qty'] = $val;
			$itemArr['created'] = $cdate;
			$obj = new Product_Service_ReceivingItem;
			$itemId = $obj->add($itemArr);
		}
	}
	
	/**
	 * 更新指定的 Rma 信息
	 * 参数 $productArr: 一维数组,索引为产品id，值为 数量
	 */
	public function update($productArr,$row,$value,$field){
		if(isset($row['receiving_code'])){
			unset($row['receiving_code']);
		}
		$Obj = new Product_Model_Receiving();
		$updateId = $Obj->update($row,$value,$field);
		$info = $Obj->getByValue($value,$field);
		$Obj->getAdapter()->beginTransaction();
		
		$rowLog =array();
		$rowLog['receiving_code'] = $info['receiving_code'];
		$rowLog['rl_type'] = 0;			//0:内容修改;1:状态修改
		$rowLog['user_id'] = 0;			//用户新创建时
		$rowLog['customer_id'] = $this->_customerId;
		$rowLog['rl_note'] = '';		//待确定
		$rowLog['rl_add_time'] = date('Y-m-d H:i:s');
		$rowLog['rl_ip'] = Common_Service_Common::getIP();
		try{
			
			$res = Product_Service_ReceivingItem::delete($info['receiving_code'],'receiving_code');
			$this->createItem($productArr,$info['receiving_code']);
			$this->createRmaLog($rowLog);				//创建日志
			$Obj->getAdapter()->commit();
		}catch (Exception $e) {
			echo $e->getTraceAsString();
			echo $e->getMessage();
			$Obj->getAdapter()->rollBack();
			return array('ask'=>0,'msg'=>'Edit asn failed');
		}
		
		return array('ask'=>'Success','receiving_code'=>$info['receiving_code'],'msg'=>'Success');
	}
	
	/**
	 * 	检查 receiving 表 ref_id 字段值(客户参考号)的唯一性
	 *  $reference_no已存在返回 false;不存在返回 true; 
	 */
	public function checkReferenceNo($reference_no){
		$obj = Product_Service_Receiving::getInstance();
		$row = $obj->getByValue($reference_no,'ref_id');
		if($row){
			return false;
		}else {
			return true;
		}		
	}
	
	/**
	 * 	针对指定用户，生成一个新的相应 code 
	 */
	public function createCode(){
		$lastCode = $ASNNo = $tmp = '';
		$obj = Product_Service_Receiving::getInstance();
		// 退货
		$lastCode = $obj->getCurdateLastReturnCode($this->_customerId);			
		$ASNNo = 'M'.$this->_customerId.date('ymd');
		if($lastCode){
			$tmp= (int) substr($lastCode,-3,3);				
			$tmp= str_pad(($tmp+1),3,'0',STR_PAD_LEFT);
		}else{
			$tmp ='001';
		}
		$ASNNo .= $tmp;		
		return $ASNNo;
	}
	
	public function getErrorVal(){
		return $this->_error;
	}
	
	public function updatePlanItem($customerId,$procType='item',$qty=0,$refCode){
		$plan = new Plan_Service_Plan();
		$plan->planProccessing($customerId,$procType,$qty,$refCode);		
	}
	
	/**
	 * 后台通过提交订单号创建 RMA 信息时，检查订单号
	 * 
	 */
	public static function checkOrderCode($orderCode){
		$error = array();
		$orderInfo = Order_Service_Order::getByOrdersCode($orderCode);	//订单信息
		if(!$orderInfo){
			$error[] = '订单号错误.';
		}elseif ($orderInfo['process_status'] != 5){
			$error[] = '该订单并未发货，无法创建退货单.';
		}
		return $error;
	}
	
	/**
	 * 	创建日志
	 */
	public function createRmaLog($row){
		$res = Product_Service_ReceivingLog::add($row);		
	}
	
	 
}
?>